package com.example.li_han.servlet;

import com.example.li_han.pojo.PingLun;
import com.example.li_han.service.PingLunService;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//已审核评论
@WebServlet(name = "GetApprovedPingLun", value = "/GetApprovedPingLun")
public class GetApprovedPingLun extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        Map<String, Object> resultMap = new HashMap<>();

        try {
            int currentPage = req.getParameter("page") == null ? 1 : Integer.parseInt(req.getParameter("page"));
            int pageSize = req.getParameter("limit") == null ? 10 : Integer.parseInt(req.getParameter("limit"));
            PingLunService pingLunService = new PingLunService();
            List<PingLun> approvedList = pingLunService.getApprovedComments(currentPage, pageSize); // 临时使用，需要扩展Service

            // 过滤出已审核的评论（状态为1）
            approvedList.removeIf(comment -> comment.getStatus() != 1);

            resultMap.put("code", 200);
            resultMap.put("msg", "获取成功");
            resultMap.put("data", approvedList);
            resultMap.put("count", approvedList.size());

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "获取失败: " + e.getMessage());
            resultMap.put("data", new java.util.ArrayList<>());
            resultMap.put("count", 0);
        }

        Gson gson = new Gson();
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
}